<?php
/*
 * Created on Dec 15, 2007
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
?>
<?php

/**
 * TODO:
 * 	Ajax Completer
 */

$format = dPgetParam($_POST, 'format', $_GET['format']);
$keyword = dPgetParam($_POST, 'keyword', $_GET['keyword']);
$obj_search = dPgetParam($_POST, 'obj_search', $_GET['obj_search']);
$all_search = true;
if(''!=$keyword){
	$result = array();
	$quotations = array();
	$references = array();
	$tasks = array();
	// If is search all
	if('all'!=$obj_search){
		$all_search = false;
	}
	if(($obj_search == 'quotations') || $all_search){
		$q = new DBQuery();
		$q->addTable('quotations');
		$q->addQuery('quote_id as id, quote_code as code, quote_name as name');
		$q->addWhere('quote_name like \'%'. $keyword . '%\'');
		$sql = $q->prepare();
		$q->clear();
		$quotations = db_loadList($sql);
		// Add link to link
		for($i = 0; $i < count($quotations); $i++){
			$quotations[$i]['link'] = 'index.php?m=quotations&a=quote_view&quote_id='.$quotations[$i]['id'];
			// TODO: group quotation to element group
			$quotations[$i]['group_name'] = 'Quotation';
		}
	}
	if(($obj_search == 'references') || $all_search){
		// Add link to link
		$q = new DBQuery();
		$q->addTable('projects');
		$q->addQuery('distinct project_id as id, project_code as code, project_name as name');
		$q->addWhere("project_code LIKE '%$keyword%' or project_name LIKE '%$keyword%'");
		$q->addOrder('project_name');
		$sql = $q->prepare();
		$q->clear();
		$references = db_loadList($sql);
		for($i = 0; $i < count($references); $i++){
//				$tmp = new stdClass();
//				$tmp->id = $projects[$i]['project_id'];
//				$tmp->code = $projects[$i]['project_code'];
//				$tmp->name = $projects[$i]['project_name'];
//				$tmp->link = 'index.php?m=projects&a=view&project_id='.$projects[$i]['project_id'];
			$references[$i]['link'] = 'index.php?m=projects&a=view&project_id='.$references[$i]['id'];
			// TODO: group quotation to element group
			$references[$i]['group_name'] = 'Reference';
		}
	}

	if(($obj_search == 'tasks') || $all_search){
		// Add link to link
		$q = new DBQuery();
		$q->addTable('tasks');
		$q->innerJoin('projects', 'pro', 'pro.project_id = task_project');
		$q->leftJoin('quotations', 'quo', 'quo.quote_id = task_quotation ');
		$q->addQuery('distinct tasks.task_id as id, tasks.task_name as name, pro.project_name');
		$q->addWhere("(task_name LIKE '%$keyword%')");
		$q->addOrder('task_name');
		$sql = $q->prepare();
		$q->clear();
		$tasks = db_loadList($sql);
		for($i = 0; $i < count($tasks); $i++){
			$tasks[$i]['link'] = 'index.php?m=tasks&a=view&task_id='.$tasks[$i]['id'];
			$tasks[$i]['code'] = $tasks[$i]['project_name'];
			// TODO: group quotation to element group
			$tasks[$i]['group_name'] = 'Task';
		}
	}

// Merge result
$result = array_merge($quotations, $references, $tasks);
//if(extension_loaded('json')){
echo json_encode($result);
//}
}
echo '';exit;
?>